<?php
/**
 * StoredObject.class.php
 *
 * PHP version 5.3
 *
 * @author		Leo Valen <leo@valen.pro>
 */
/**
 * Base class for objects that are stored somewhere and kept record of in a common table
 *
 * @version		1.0
 */
namespace TeamPlayers;
abstract class StoredObject extends Object
{
	/**
	 * Human readable description of the object
	 *
	 * @var string
	 * @access protected
	 */
	protected $description;
	/**
	 * The object's class
	 *
	 * @var string
	 * @access protected
	 */
	protected $class;
	/**
	 * Object creation timestamp
	 *
	 * @var string
	 * @access protected
	 */
	protected $created;
	/**
	 * Object modification timestamp
	 *
	 * @var string
	 * @access protected
	 */
	protected $modified;
	/**
	 * Fetches metadata for the object if uuid is specified and exists, or creates a new metadata record if the object is new
	 *
	 * @param       string $uuid
	 * @return		void
	 * @access		public
	 */
	function __construct( $uuid = false )
	{
		parent::__construct( $uuid );
		if ( ! $this->fetchMetadata() || $uuid === false )
		{
		    $this->createMetadataRecord();
		}
	}
	/**
	 * Removes the metadata record for this object.
	 *
	 * @return		bool
	 * @access		public
	 */
	public function destroy()
	{
		return TeamPlayers::$db->query( "DELETE FROM `StoredObjects` WHERE `uuid` = '{$this->uuid}'" ) ? true : false;
	}
	/**
	 * Sets the human readable description text for this object
	 *
	 * @param       string $description
	 * @return		bool
	 * @access		public
	 */
	public function setDescription( $description )
	{
	    if ( is_string( $description ) )
	    {
	        $this->description = $description;
	        return TeamPlayers::$db->query( "UPDATE `StoredObjects` SET `description` = '{$description}', `modified` = NOW() WHERE `uuid` = '{$this->uuid}'" ) ? true : false;
	    }
	}
	/**
	 * Gets the human readable description text for this object
	 *
	 * @return		string
	 * @access		public
	 */
	public function getDescription()
	{
	    return $this->description;
	}
	/**
	 * Fetches metadata for this instance if is exists. Returns false if the UUID doesn't exists
	 *
	 * @param       string $description
	 * @return		void
	 * @access		protected
	 */
	protected function fetchMetadata()
	{
	    $sql = "SELECT
	                `description`, `class`, UNIX_TIMESTAMP( `created` ) AS `created`, UNIX_TIMESTAMP( `modified` ) AS `modified`
	            FROM
	                `StoredObjects`
	            WHERE
	                `uuid` = '{$this->uuid}'";
	    if ( $result = TeamPlayers::$db->selectAssoc( $sql ) )
	    {
	        $meta = $result[0];
	    }
	    else
	    {
	        return false;
	    }
	    $this->description = $meta['description'];
	    $this->class = $meta['class'];
	    $this->created = $meta['created'];
	    $this->modified = $meta['modified'];
	}
	/**
	 * Creates a record of this object in the metadata table
	 *
	 * @param       string $description
	 * @return		bool
	 * @access		protected
	 */
	protected function createMetadataRecord()
	{
	    $class = TeamPlayers::$db->real_escape_string( get_class( $this ) );
	    $sql = "INSERT INTO `StoredObjects` SET `uuid` = '{$this->uuid}', `description` = '', `class` = '{$class}', `modified` = NOW(), `created` = NOW()";
	    return TeamPlayers::$db->query( $sql ) ? true : false;
	}
}
?>
